package henu.action;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

import henu.bean.Communictor;
import henu.bean.FeedBack;
import henu.bean.Lesson;
import henu.bean.Notice;
import henu.bean.Schedule;
import henu.bean.TClass;
import henu.bean.Teacher;
import henu.bean.User;
import henu.factory.DaoFactory;
import henu.util.ExportExcel;

public class ExportAction extends ActionSupport {
	private InputStream excelFile;
	HttpServletRequest request = ServletActionContext.getRequest();

	public String execute() throws Exception {
		String name = request.getParameter("name");
		if (name.equals("notice")) {
			NoticeExcelFile();
			return "notice";
		}
		if (name.equals("teacher")) {
			teacherExport();
			return "teacher";
		}
		if (name.equals("lesson")) {
			lessonExport();
			return "lesson";
		}
		if (name.equals("schedule")) {
			scheduleExport();
			return "schedule";
		}
		if(name.equals("educlass")){
			classExport();
			return "educlass";
		}
		
		if(name.equals("user")){
			userExport();
			return "user";
		}
		if(name.equals("communictor")){
			communictorExport();
			return "communictor";
		}
		if(name.equals("feedback")){
			feedbackExport();
			return "feedback";
		}
		return ERROR;
	}

	// ++++++++++++++++++++++++++
	/*
	 * 数据库 查询后绑定到对象后 将对象赋值给list 然后转成对象进行列操作
	 */
	public void NoticeExcelFile() {
		Workbook workbook = null;
		// 从数据库获取Notice的list
		//
		List<Notice> list = new ArrayList();
		list = DaoFactory.createNoticeDao().getAll();
		workbook = new HSSFWorkbook();

		Sheet sheet = workbook.createSheet("公告信息");
		Row row = sheet.createRow(0);
		row.createCell(0).setCellValue("标题");
		row.createCell(1).setCellValue("内容");
		row.createCell(2).setCellValue("发布人");
		row.createCell(3).setCellValue("时间");
		CellStyle cellStyle = workbook.createCellStyle();
		cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy"));
		for (int i = 1; i <= list.size(); i++) {
			Notice att = list.get(i - 1);
			row = sheet.createRow(i);
			row.createCell(0).setCellValue(att.getfTitle());
			row.createCell(1).setCellValue(att.getfContent());
			row.createCell(2).setCellValue(att.getfPublisher());
			row.createCell(3).setCellValue(att.getfTime());
		}
		// 让列宽随着导出的列长自动适应
		for (int colNum = 0; colNum < 4; colNum++) {
			int columnWidth = sheet.getColumnWidth(colNum) / 256 + 10;
			for (int rowNum = 0; rowNum < sheet.getLastRowNum(); rowNum++) {
				HSSFRow currentRow;
				// 当前行未被使用过
				if (sheet.getRow(rowNum) == null) {
					currentRow = (HSSFRow) sheet.createRow(rowNum);
				} else {
					currentRow = (HSSFRow) sheet.getRow(rowNum);
				}
				if (currentRow.getCell(colNum) != null) {
					HSSFCell currentCell = currentRow.getCell(colNum);
					if (currentCell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
						int length = currentCell.getStringCellValue().getBytes().length;
						if (columnWidth < length) {
							columnWidth = length;
						}
					}
				}
			}
			if (colNum == 0) {
				sheet.setColumnWidth(colNum, (columnWidth - 2) * 256);
			} else {
				sheet.setColumnWidth(colNum, (columnWidth + 4) * 256);
			}
		}
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		try {

			workbook.write(baos);

		} catch (IOException e) {
			e.printStackTrace();
		}
		byte[] aa = baos.toByteArray();
		excelFile = new ByteArrayInputStream(aa, 0, aa.length);
		try {
			baos.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public void teacherExport() {
		Workbook workbook = null;
		// 从数据库获取Notice的list
		//
		List<Teacher> list = new ArrayList();
		list = DaoFactory.createTeacherDao().getAll();
		workbook = new HSSFWorkbook();
		Sheet sheet = workbook.createSheet("教师信息");
		Row row = sheet.createRow(0);
		row.createCell(0).setCellValue("教工号");
		row.createCell(1).setCellValue("教师姓名");
		row.createCell(2).setCellValue("职称");
		row.createCell(3).setCellValue("系别");
		row.createCell(4).setCellValue("联系电话");
		row.createCell(5).setCellValue("邮箱");
		row.createCell(6).setCellValue("QQ");
		row.createCell(7).setCellValue("备注");
		CellStyle cellStyle = workbook.createCellStyle();
		cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy"));
		for (int i = 1; i <= list.size(); i++) {
			Teacher att = list.get(i - 1);
			row = sheet.createRow(i);
			row.createCell(0).setCellValue(att.getfTeacherId());
			row.createCell(1).setCellValue(att.getfTeachName());
			row.createCell(2).setCellValue(att.getfJobTitle());
			row.createCell(3).setCellValue(att.getfDept());
			row.createCell(4).setCellValue(att.getfPhoneNumber());
			row.createCell(5).setCellValue(att.getfEmail());
			row.createCell(6).setCellValue(att.getfQQ());
			row.createCell(7).setCellValue(att.getfMemo());
		}
		// 让列宽随着导出的列长自动适应
		for (int colNum = 0; colNum < 4; colNum++) {
			int columnWidth = sheet.getColumnWidth(colNum) / 256 + 10;
			for (int rowNum = 0; rowNum < sheet.getLastRowNum(); rowNum++) {
				HSSFRow currentRow;
				// 当前行未被使用过
				if (sheet.getRow(rowNum) == null) {
					currentRow = (HSSFRow) sheet.createRow(rowNum);
				} else {
					currentRow = (HSSFRow) sheet.getRow(rowNum);
				}
				if (currentRow.getCell(colNum) != null) {
					HSSFCell currentCell = currentRow.getCell(colNum);
					if (currentCell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
						int length = currentCell.getStringCellValue().getBytes().length;
						if (columnWidth < length) {
							columnWidth = length;
						}
					}
				}
			}
			if (colNum == 0) {
				sheet.setColumnWidth(colNum, (columnWidth - 2) * 256);
			} else {
				sheet.setColumnWidth(colNum, (columnWidth + 4) * 256);
			}
		}
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		try {

			workbook.write(baos);

		} catch (IOException e) {
			e.printStackTrace();
		}
		byte[] aa = baos.toByteArray();
		excelFile = new ByteArrayInputStream(aa, 0, aa.length);
		try {
			baos.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void lessonExport() {
		List<Lesson> list = new ArrayList();
		list = DaoFactory.createLessonDao().getAll();
		String title = "课程信息";
		String[] rowName = { "课程名称", "课程编号", "理论周学时", "实验周学时", "教学起始周", "教学结束周", "学年", "学期", "备注" };
		List<Object[]> dataList = new ArrayList<Object[]>();
		for (int i = 0; i < list.size(); i++) {
			Object[] obj = new Object[rowName.length];
			for (int j = 0; j < rowName.length; j++) {
				if (j == 0) {
					obj[j] = list.get(i).getfClassName();
				}
				if (j == 1) {
					obj[j] = list.get(i).getfClassNumber();
				}
				if (j == 2) {
					obj[j] = list.get(i).getfTheoryTime();
				}
				if (j == 3) {
					obj[j] = list.get(i).getfTestTime();
				}
				if (j == 4) {
					obj[j] = list.get(i).getfStartWeek();
				}
				if (j == 5) {
					obj[j] = list.get(i).getfEndWeek();
				}
				if (j == 6) {
					obj[j] = list.get(i).getfSchoolYear();
				}
				if (j == 7) {
					obj[j] = list.get(i).getfSchoolTerm();
				}
				if (j == 8) {
					obj[j] = list.get(i).getfRemark();
				}
				dataList.add(obj);
			}
		}
		ExportExcel exportEx = new ExportExcel(title, rowName, dataList);
		try {
			exportEx.export();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		excelFile = exportEx.getExcelFile();

	}

	public void scheduleExport() {
		List<Schedule> list = new ArrayList();
		list = DaoFactory.createScheduleDao().getAll();
		String title = "课表信息";
		String[] rowName = { "课程号", "课程名称", "开课学年", "开课学期", "学分" };
		List<Object[]> dataList = new ArrayList<Object[]>();
		for (int i = 0; i < list.size(); i++) {
			Object[] obj = new Object[rowName.length];
			for (int j = 0; j < rowName.length; j++) {
				if (j == 0) {
					obj[j] = list.get(i).getfCourseNum();
				} else if (j == 1) {
					obj[j] = list.get(i).getfCourseName();
				} else if (j == 2) {
					obj[j] = list.get(i).getfCourseYear();
				} else if (j == 3) {
					obj[j] = list.get(i).getfCourseTerm();
				} else if (j == 4) {
					obj[j] = list.get(i).getfGrade();
				}
			}
			dataList.add(obj);
		}
		ExportExcel exportEx = new ExportExcel(title, rowName, dataList);
		try {
			exportEx.export();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		excelFile = exportEx.getExcelFile();

	}

	public void classExport() {
		List<TClass> list = new ArrayList();
		list = DaoFactory.createTClassDao().getAll();
		String title = "班级信息";
		String[] rowName = { "班级名称", "专业", "年级", "班级人数", "辅导员", "班长", "班长联系电话", "备注" };
		List<Object[]> dataList = new ArrayList<Object[]>();
		for (int i = 0; i < list.size(); i++) {
			Object[] obj = new Object[rowName.length];
			for (int j = 0; j < rowName.length; j++) {
				if (j == 0) {
					obj[j] = list.get(i).getfClass();
				}
				if (j == 1) {
					obj[j] = list.get(i).getfMajor();
				}
				if (j == 2) {
					obj[j] = list.get(i).getfGrade();
				}
				if (j == 3) {
					obj[j] = list.get(i).getfNumber();
				}
				if (j == 4) {
					obj[j] = list.get(i).getfCoach();
				}
				if (j == 5) {
					obj[j] = list.get(i).getfMonitor();
				}
				if (j == 6) {
					obj[j] = list.get(i).getfMonitorPhone();
				}
				if (j == 7) {
					obj[j] = list.get(i).getfMemo();
				}
			}
			dataList.add(obj);
		}
		ExportExcel exportEx = new ExportExcel(title, rowName, dataList);
		try
		{
			exportEx.export();
		}catch(
		Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		excelFile=exportEx.getExcelFile();
		}
	
	public void userExport(){
		List<User> list = new ArrayList();
		list = DaoFactory.createUserDao().getAll();
		String title = "用户信息";
		String[] rowName = { "用户名", "姓名", "密码", "提示问题", "提示答案", "用户类型","备注" };
		List<Object[]> dataList = new ArrayList<Object[]>();
		for (int i = 0; i < list.size(); i++) {
			Object[] obj = new Object[rowName.length];
			for (int j = 0; j < rowName.length; j++) {
				if (j == 0) {
					obj[j] = list.get(i).getfUsername();
				}
				if (j == 1) {
					obj[j] = list.get(i).getfRealname();
				}
				if (j == 2) {
					obj[j] = list.get(i).getfPassword();
				}
				if (j == 3) {
					obj[j] = list.get(i).getfReminderQuestion();
				}
				if (j == 4) {
					obj[j] = list.get(i).getfReminderAnswer();
				}
				if (j == 5) {
					obj[j] = list.get(i).getfUserType();
				}
				if (j == 6) {
					obj[j] = list.get(i).getfRemark();
				}
			}
			dataList.add(obj);
		}
		ExportExcel exportEx = new ExportExcel(title, rowName, dataList);
		try
		{
			exportEx.export();
		}catch(
		Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		excelFile=exportEx.getExcelFile();
	}
	
	public void communictorExport(){
		List<Communictor> list = new ArrayList();
		list = DaoFactory.createCommunictorDao().getAll();
		String title = "联络员信息";
		String[] rowName = { "学号", "姓名", "密码", "班级名称", "QQ", "邮箱","联系电话","主要负责课程","次数统计" };
		List<Object[]> dataList = new ArrayList<Object[]>();
		for (int i = 0; i < list.size(); i++) {
			Object[] obj = new Object[rowName.length];
			for (int j = 0; j < rowName.length; j++) {
				if (j == 0) {
					obj[j] = list.get(i).getfId();
				}
				if (j == 1) {
					obj[j] = list.get(i).getfName();
				}
				if (j == 2) {
					obj[j] = list.get(i).getfPassword();
				}
				if (j == 3) {
					obj[j] = list.get(i).getfClass();
				}
				if (j == 4) {
					obj[j] = list.get(i).getfQQ();
				}
				if (j == 5) {
					obj[j] = list.get(i).getfEmail();
				}
				if (j == 6) {
					obj[j] = list.get(i).getfPhone();
				}
				if (j == 7) {
					obj[j] = list.get(i).getfMianSubject();
				}
				if (j == 8) {
					obj[j] = list.get(i).getfMemo();
				}
			}
			dataList.add(obj);
		}
		ExportExcel exportEx = new ExportExcel(title, rowName, dataList);
		try
		{
			exportEx.export();
		}catch(
		Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		excelFile=exportEx.getExcelFile();
	}
	
	public void feedbackExport(){
		List<FeedBack> list = new ArrayList();
		list = DaoFactory.createFeedBackDao().getAll();
		String title = "反馈信息";
		String[] rowName = { "周次", "上课时间", "课程名", "任课教师", "反馈", "学院意见" };
		List<Object[]> dataList = new ArrayList<Object[]>();
		for (int i = 0; i < list.size(); i++) {
			Object[] obj = new Object[rowName.length];
			for (int j = 0; j < rowName.length; j++) {
				if (j == 0) {
					obj[j] = list.get(i).getfWeekTime();
				}
				if (j == 1) {
					obj[j] = list.get(i).getfClassOrd();
				}
				if (j == 2) {
					obj[j] = list.get(i).getfLessonName();
				}
				if (j == 3) {
					obj[j] = list.get(i).getfTeacherName();
				}
				if (j == 4) {
					obj[j] = list.get(i).getfSchedule()+list.get(i).getfClassSitutation()+list.get(i).getfClassEfect()+
							list.get(i).getfAfterClassSitu()+list.get(i).getfAdvice();
				}
				if (j == 5) {
					obj[j] = list.get(i).getfHandlingSuggestion();
				}
				
			}
			dataList.add(obj);
		}
		ExportExcel exportEx = new ExportExcel(title, rowName, dataList);
		try
		{
			exportEx.export();
		}catch(
		Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		excelFile=exportEx.getExcelFile();
	}
	
	public InputStream getExcelFile() {
		return excelFile;
	}
}
